﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using Agile.Paypal.Web.Services;

namespace Agile.Paypal.Web
{
    public class PaypalApp
    {
        private readonly Func<IPaypalNotificationProcessor> _procesorResolver;
        public static string IpnUrl { get; private set; }
        public PaypalApp(string url,Func<IPaypalNotificationProcessor> procesorResolver)
        {
            _procesorResolver = procesorResolver;
            IpnUrl = url.Trim('/', '\\');
        }

        public void Init()
        {
            GenerateRoute(RouteTable.Routes);
        }

        protected virtual void GenerateRoute(RouteCollection routes)
        {
            RouteValueDictionary defaults = new RouteValueDictionary();
            defaults.Add("action", null);
            defaults.Add("controller", null);

            routes.Insert(0, new Route(IpnUrl, defaults, new PaypalIpnRouteHandler(_procesorResolver.Invoke())));
        }

      
    }
}
